package com.carkeeper.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpCookie;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

@Service
public class AuthService {
    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     *  查看cookie中的jti是否存在
     * @param request
     * @return
     */
    public String getJtiFromCookie(ServerHttpRequest request) {
        //1.获取cookie的集合
        MultiValueMap<String, HttpCookie> cookies = request.getCookies();
        //从cookie中获取短令牌jti
        HttpCookie cookie = cookies.getFirst("uid");
        if (cookie != null) {
            //返回短令牌
            return cookie.getValue();
        }
        return null;
    }

    /**
     *  查看redis中的token是否已经过期
     * @param jti
     * @return
     */
    public String getTokenFromRedis(String jti) {
        //查询redis中是否有这个String类型的jti 令牌
        String token = redisTemplate.boundValueOps(jti).get();
        return token;
    }
}

